<?php

namespace Home\Controller;

class UserController extends CommonController {
	public function __construct() {
		parent::__construct ();
		parent::__config ( array (
				'model_name' => 'User' 
		) );
	}
	public function verify_c() {
		$Verify = new \Think\Verify ();
		$Verify->fontSize = 20;
		$Verify->length = 4;
		$Verify->fontttf = '5.ttf';
		$Verify->useCurve = false;
		$Verify->entry ();
	}
	public function index() {
		$this->display ("register");
	}
	public function ad() {
		$this->display ();
	}
	public function newsadright() {
		$this->display ();
	}
	public function register() {
		$code = I("code");
		//没带参数就获取session的
		if(!$code){
			$code = session('lj_code');
		}
		session('lj_code',$code);
		$agent_code = decrypt2($code);
		$this->assign("agent_code",$agent_code);
		$this->display ();
	}
	public function changpwd() {
		$this->display ();
	}
	public function getpwd() {
		$this->display ();
	}
	
	public function agent_getpwd(){
		$this->display();
	}
// 	public function changpaypwd() {
// 		$this->display ();
// 	}
// 	public function getpaypwd() {
// 		$this->display ();
// 	}
	public function contact() {
		$this->display ();
	}
	public function cooperate() {
		$this->display ();
	}
	public function download() {
		$this->display ();
	}
	public function download_cqssc() {
		$this->display ();
	}
	public function agent() {
		$this->display ();
	}
	public function agent_register() {
		$this->display ();
	}
	
	public function checkregister(){
		$username= mysql_real_escape_string(I("username"));
		$userModel = D("User");
		$where['username']=$username;
		$result = $userModel->where($where)->find();
		if($result){
			echo 2;
		}else{
			echo 1;
		}
	}
	
	public function register_post() {
		// dump($_POST);
		// die();
		$Verify = new \Think\Verify ();
		$agent_code = I("agent_code");
		$agent_code_md5 = encrypt2($agent_code);
		if ($Verify->check ( I ( 'code' ) )) {
			if (I ( 'username' ) == null || I ( 'email' ) == null || I ( 'qq' ) == null || I ( 'password' ) == null) {
				echo "<script>alert('数据填写不对,请重新填写!');location.href='" . __CONTROLLER__ . "/register?code=".$agent_code_md5."'</script>";
				die();
			}
			if (I ( 'password' ) != I ( 'password2' ) || I ( 'email' ) != I ( 'email2' )) {
				echo "<script>alert('数据填写不对,请重新填写!');location.href='" . __CONTROLLER__ . "/register?code=".$agent_code_md5."'</script>";
				die();
			}
			// 过滤邮箱
			$pattern = "/([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?/i";
			if (! preg_match ( $pattern, I ( 'email' ) )) {
				echo "<script>alert('数据填写不对,请重新填写!');location.href='" . __CONTROLLER__ . "/register?code=".$agent_code_md5."'</script>";
				die();
			}
			//查询是否有改代理
			$where_agent['agent_code']=$agent_code;
			$agent_id = D("Agent")->where($where_agent)->field('agent_id')->find();
			$adminModel = D ( "User" );
			// 获取当前注册过ip和邮箱，一个超过20就不给注册
			$where_register_num ['register_ip'] = get_real_ip ();
			$where_register_num ['email'] = I ( 'email' );
			$where_register_num ['_logic'] = 'or';
			$resigter_num = $adminModel->where ( $where_register_num )->count ();
			if ($resigter_num > 20) {
				echo "<script>alert('注册过于频繁，请稍后再试!');location.href='" . __CONTROLLER__ . "/register?code=".$agent_code_md5."'</script>";
				die();
			}
			$where_find ['username'] = I ( 'username' );
			if ($adminModel->where ( $where_find )->find ()) {
				echo "<script>alert('该用户已经被注册');location.href='" . __CONTROLLER__ . "/register?code=".$agent_code_md5."'</script>";
				die();
			} else {
				$data_add ['username'] = I ( 'username' );
				$data_add ['password'] = encrypt ( I ( 'password' ) );
				// $data_add ['headUrl'] = "/matrix/upload/user/user.jpg";
				$data_add ['email'] = I ( 'email' );
				$data_add ['qq'] = I ( 'qq' );
				$data_add ['phone'] = I ( 'phone' );
				$data_add ['createtime'] = time();
				$data_add ['register_ip'] = get_real_ip ();
				$data_add ['agent_id'] = $agent_id['agent_id'];
				$user_id = $adminModel->add ( $data_add );
				if ($user_id) {
					$user ['id'] = $user_id;
					$user ['username'] = I ( 'username' );
					session ( 'user', $user );
					echo "<script>alert('" . I ( 'username' ) . "注册成功！');location.href='" . __CONTROLLER__ . "/login_pay'</script>";
					die();
				} else {
					echo "<script>alert('" . I ( 'username' ) . "注册失败！');location.href='" . __CONTROLLER__ . "/register?code=".$agent_code_md5."'</script>";
					die();
				}
			}
		} else {
			echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/register?code=".$agent_code_md5."'</script>";
			die();
		}
	}
	function chang_pwd() {
		$Verify = new \Think\Verify ();
		if ($Verify->check ( I ( 'code' ) )) {
			if (I ( 'username' ) == null || I ( 'old_password' ) == null || I ( 'new_password' ) == null || I ( 'new_password2' ) == null || I ( 'code' ) == null) {
				die ();
			}
			if (I ( 'new_password' ) != I ( 'new_password2' )) {
				die ();
			}
			$adminModel = D ( "User" );
			$where_find ['username'] = I ( 'username' );
			$user_find = $adminModel->where ( $where_find )->find ();
			
			if (! $user_find) {
				echo "<script>alert('用户错误!');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
			} else {
				// var_dump(decrypt ( $user_find ['password'] ));
				// var_dump(I ( 'old_password' ));
				// die();
				if (decrypt ( $user_find ['password'] ) == I ( 'old_password' )) { // 旧密码相同
					$data_add ['id'] = $user_find ['id'];
					$data_add ['password'] = encrypt ( I ( 'new_password' ) );
					$save_res = $adminModel->save ( $data_add );
					if ($save_res !== false) {
						echo "<script>alert('用户：" . I ( 'username' ) . " 修改成功！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
					} else {
						echo "<script>alert('" . I ( 'username' ) . "修改失败！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
					}
				} else {
					echo "<script>alert('" . I ( 'username' ) . "旧密码错误！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
				}
			}
		} else {
			echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
		}
	}
// 	function chang_pay_pwd() {
// 		$Verify = new \Think\Verify ();
// 		if ($Verify->check ( I ( 'code' ) )) {
			
// 			if (I ( 'username' ) == null || I ( 'old_pay_password' ) == null || I ( 'new_pay_password' ) == null || I ( 'new_pay_password2' ) == null || I ( 'code' ) == null) {
// 				die ();
// 			}
// 			if (I ( 'new_pay_password' ) != I ( 'new_pay_password2' )) {
// 				die ();
// 			}
// 			$adminModel = D ( "User" );
// 			$where_find ['username'] = I ( 'username' );
// 			$user_find = $adminModel->where ( $where_find )->find ();
// 			if (! $user_find) {
// 				echo "<script>alert('用户错误!');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
// 			} else {
// 				if (decrypt ( $user_find ['pay_password'] ) == I ( 'old_pay_password' )) { // 旧密码相同
// 					$data_add ['id'] = $user_find ['id'];
// 					$data_add ['pay_password'] = encrypt ( I ( 'new_pay_password' ) );
// 					$save_res = $adminModel->save ( $data_add );
// 					if ($save_res !== false) {
// 						echo "<script>alert('用户：" . I ( 'username' ) . " 修改成功！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
// 					} else {
// 						echo "<script>alert('" . I ( 'username' ) . "修改失败！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
// 					}
// 				} else {
// 					echo "<script>alert('" . I ( 'username' ) . "旧密码错误！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
// 				}
// 			}
// 		} else {
// 			echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/changpwd'</script>";
// 		}
// 	}
	function get_pwd() {
		$Verify = new \Think\Verify ();
		if ($Verify->check ( I ( 'code' ) )) {
			if (I ( 'username' ) == null || I ( 'email' ) == null) {
				die ();
			}
			$adminModel = D ( "User" );
			$where_find ['username'] = I ( 'username' );
			$where_find ['email'] = I ( 'email' );
			$user_find_res = $adminModel->where ( $where_find )->find ();
			if (! $user_find_res) {
				echo "<script>alert('用户和邮箱对不上!');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
			} else {
				// 查找2小时内有没发超过10,没则插入找回密码表，继续发
				$emailsendModel = D ( "Emailsend" );
				$where_num ['email'] = $_POST ['email'];
				$where_num ['username'] = $_POST ['username'];
				$where_num ['time'] = array (
						'gt',
						time () 
				);
				$num = $emailsendModel->where ( $where_num )->count ();
				if ($num > 3) {
					echo "<script>alert('发送频率太高,请2小时后再尝试！');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
					die ();
				}
				$token = base64_encode ( $_POST ['email'] );
				$time = time () + C ( 'EMAIL_RESET_TIMEOUT' ); // +2小时
				$data_e_s ['email'] = $_POST ['email'];
				$data_e_s ['username'] = $_POST ['username'];
				$data_e_s ['token'] = $token;
				$data_e_s ['time'] = $time; // +2小时
				$result_add = $emailsendModel->add ( $data_e_s );
				$to = $_POST ['email'];
				$title = "盈彩 密码帮助";
				$content = "亲爱的用户！取回通行证密码为：" . decrypt ( $user_find_res ['password'] ) . "。请妥善保管，及时销毁邮件，并注意您的账号安全。此致:盈彩支援团队。
				系统邮件，请勿直接回复。";
				$result = sendMail ( $to, $title, $content );
				// die();
				if ($result) {
					echo "<script>alert('邮件发送成功！请到" . $_POST ['email'] . "邮箱查收,请勿重复操作！');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
				} else {
					echo "<script>alert('邮件发送失败!');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
				}
			}
		} else {
			echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
		}
	}
	
	
	//代理找回密码发送邮件
	function agent_get_pwd() {
		$Verify = new \Think\Verify ();
		if ($Verify->check ( I ( 'code' ) )) {
			if (I ( 'username' ) == null || I ( 'email' ) == null) {
				die ();
			}
			$adminModel = D ( "Agent" );
			$where_find ['username'] = I ( 'username' );
			$where_find ['email'] = I ( 'email' );
			$user_find_res = $adminModel->where ( $where_find )->find ();
			if (! $user_find_res) {
				echo "<script>alert('代理账号和邮箱对不上!');location.href='" . __CONTROLLER__ . "/agent_getpwd'</script>";
			} else {
				// 查找2小时内有没发超过10,没则插入找回密码表，继续发
				$emailsendModel = D ( "Emailsend" );
				$where_num ['email'] = $_POST ['email'];
				$where_num ['username'] = $_POST ['username'];
				$where_num ['time'] = array (
						'gt',
						time ()
				);
				$num = $emailsendModel->where ( $where_num )->count ();
				if ($num > 3) {
					echo "<script>alert('发送频率太高,请2小时后再尝试！');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
					die ();
				}
				$token = base64_encode ( $_POST ['email'] );
				$time = time () + C ( 'EMAIL_RESET_TIMEOUT' ); // +2小时
				$data_e_s ['email'] = $_POST ['email'];
				$data_e_s ['username'] = $_POST ['username'];
				$data_e_s ['token'] = $token;
				$data_e_s ['time'] = $time; // +2小时
				$result_add = $emailsendModel->add ( $data_e_s );
				$to = $_POST ['email'];
				$title = "盈彩 密码帮助";
				$content = "亲爱的用户！取回代理账号密码为：" . decrypt ( $user_find_res ['password'] ) . "。请妥善保管，及时销毁邮件，并注意您的账号安全。此致:盈彩支援团队。
				系统邮件，请勿直接回复。";
				$result = sendMail ( $to, $title, $content );
				// die();
				if ($result) {
					echo "<script>alert('邮件发送成功！请到" . $_POST ['email'] . "邮箱查收,请勿重复操作！');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
				} else {
					echo "<script>alert('邮件发送失败!');location.href='" . __CONTROLLER__ . "/getpwd'</script>";
				}
			}
		} else {
			echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/agent_getpwd'</script>";
		}
	}
// 	function get_pay_pwd() {
// 		$Verify = new \Think\Verify ();
// 		if ($Verify->check ( I ( 'code' ) )) {
// 			if (I ( 'username' ) == null || I ( 'email' ) == null) {
// 				die ();
// 			}
// 			$adminModel = D ( "User" );
// 			$where_find ['username'] = I ( 'username' );
// 			$where_find ['email'] = I ( 'email' );
// 			$user_find_res = $adminModel->where ( $where_find )->find ();
// 			if (! $user_find_res) {
// 				echo "<script>alert('用户和邮箱对不上!');location.href='" . __CONTROLLER__ . "/getpaypwd'</script>";
// 			} else {
// 				// 查找2小时内有没发超过10,没则插入找回密码表，继续发
// 				$emailsendModel = D ( "Emailsend" );
// 				$where_num ['email'] = $_POST ['email'];
// 				$where_num ['username'] = $_POST ['username'];
// 				$where_num ['time'] = array (
// 						'gt',
// 						time () 
// 				);
// 				$num = $emailsendModel->where ( $where_num )->count ();
// 				if ($num > 3) {
// 					echo "<script>alert('发送频率太高,请2小时后再尝试！');location.href='" . __CONTROLLER__ . "/getpaypwd'</script>";
// 					die ();
// 				}
// 				$token = base64_encode ( $_POST ['email'] );
// 				$time = time () + C ( 'EMAIL_RESET_TIMEOUT' ); // +2小时
// 				$data_e_s ['email'] = $_POST ['email'];
// 				$data_e_s ['username'] = $_POST ['username'];
// 				$data_e_s ['token'] = $token;
// 				$data_e_s ['time'] = $time; // +2小时
// 				$result_add = $emailsendModel->add ( $data_e_s );
// 				$to = $_POST ['email'];
// 				$title = "盈彩 密码帮助";
// 				$content = "亲爱的用户！取回通行证密码为：" . decrypt ( $user_find_res ['pay_password'] ) . "。请妥善保管，及时销毁邮件，并注意您的账号安全。此致:时时彩支援团队。
// 				系统邮件，请勿直接回复。";
// 				$result = sendMail ( $to, $title, $content );
// 				echo "<script>alert('邮件发送成功！请到" . $_POST ['email'] . "邮箱查收,请勿重复操作！');location.href='" . __CONTROLLER__ . "/getpaypwd'</script>";
// 			}
// 		} else {
// 			echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/getpaypwd'</script>";
// 		}
// 	}
	function login_pay() {
		if ($_POST ['username'] && $_POST ['password']) {
			if( !I ( 'code' ) ){
				echo "<script>alert('" . I ( 'username' ) . "验证码不能为空！');location.href='" . __CONTROLLER__ . "/login_pay'</script>";
				die();
			}
			$Verify = new \Think\Verify ();
			if ($Verify->check ( I ( 'code' ) )) {
				$adminModel = D ( "User" );
				$where_find ['username'] = I ( 'username' );
				$user_find = $adminModel->where ( $where_find )->find ();
				if (decrypt ( $user_find ['password'] ) == I ( 'password' )) {
					session ( "username", $user_find ['username'] );
					session ( "user_id", $user_find ['id'] );
					session("end_time",$user_find['end_time']);//重庆时时彩过期时间
					$this->redirect ( 'select_cz' );
				}else {
					echo "<script>alert('" . I ( 'username' ) . "密码错误！');location.href='" . __CONTROLLER__ . "/login_pay'</script>";
				}
			} else {
				echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/login_pay'</script>";
			}
		} else {
			$user_id = session("user_id");
			if($user_id){
				$this->redirect ( 'select_cz' );
			}else{
				$this->display ();
			}
		}
	}
	
	function select_cz() {
		// 需要登录验证
		$this->check_pay_login ();
		$this->assign ( 'username', session("username"));
		$this->display ();
	}
	
	function select_cz_next() {
		// 需要登录验证
		$this->check_pay_login ();
		switch ($_POST ['authorizationtype']) {
			case 6 :
				$caizhong_num = 6;
				$caizhong = "重庆时时彩盈彩计划";
				break;
			default :
				echo "<script>alert('当前彩种开发中，敬请期待！');location.href='" . __CONTROLLER__ . "/select_cz'</script>";
				die();
				break;
		}
		session("caizhong",$caizhong);
		session("caizhong_num",$caizhong_num);
		$this->redirect ( 'pay');
	}
	
	function pay() {
		// 需要登录验证
		$this->check_pay_login ();
		//到期时间
		$end_time=session("end_time");
		if($end_time){
			$this->assign ( 'end_string', "到期时间为：".date("Y-m-d H:i:s",$end_time));
		}else{
			$this->assign ( 'end_string','');
		}
		
		$this->assign ( 'username', session("username"));
		$this->assign ( 'caizhong', session("caizhong"));
		$this->display ();
	}
	
	function pay_next() {
		$this->check_pay_login ();
		if($_POST['from']!='from_pay'){
			die();
		}
		$orderModel = D("Order");
		switch ($_POST ['authorizationtype']) {
			case 7 :
				if($_POST['day_num']==null||$_POST['day_num']<=0){
					echo "<script>alert('自选至少购买1天!');location.href='" . __CONTROLLER__ . "/pay'</script>";
					die();
				}
				$money = intval($_POST['day_num'])*0.5;
				$taocan = "《盈彩》".intval($_POST['day_num'])."天授权";
				break;
			case 1 :
				$money = '10.00';
				$taocan = "《盈彩》一个月授权";
				break;
			case 2 :
				$money = '80.00';
				$taocan = "《盈彩》一年授权";
				break;
			case 5 :
				$money = '150.00';
				$taocan = "《盈彩》终身个月授权";
				break;
			case 6 :
				//判断用户是否体验过
				$userModel = D("User");
				$where_user['username']=session("username");
				$user_info = $userModel->where($where_user)->find();
				if($user_info['is_fee']){
					echo "<script>alert('该账号免费试用一天已经使用过!');location.href='" . __CONTROLLER__ . "/pay'</script>";
					die();
				}
				//检查订单里有没有
				$where_order_find['user_id']=$user_info['id'];
				$where_order_find['taocan_num']=6;
				$order_find = $orderModel->where($where_order_find)->find();
				if($order_find){
					echo "<script>alert('免费试用一天已申请过，请等待审核!');location.href='" . __CONTROLLER__ . "/pay'</script>";
					die();
				}
				$money = '0.00';
				$taocan = "《盈彩》免费试用一天";
				break;
			
			default :
				$money = '0.00';
				$taocan = "《盈彩》免费试用一天";
				//判断用户是否体验过
				$where_user['username']=session("username");
				$user_info = $userModel->where($where_user)->find();
				if($user_info['is_fee']){
					echo "<script>alert('该账号免费试用一天已经使用过!');location.href='" . __CONTROLLER__ . "/pay'</script>";
					die();
				}
				break;
		}
		
		
		//有效订单不能超过５条
		$where_find['user_id']=session("user_id");
		$where_find['add_time']=array("gt",time()-86400*2);
		$num = $orderModel->where($where_find)->count();
		if($num>=10){
			echo "<script>alert('操作太频繁，请先支付或在24小时后再试!');location.href='" . __CONTROLLER__ . "/pay_list'</script>";
			die();
		}
		//生成支付订单
		$data['user_id']=session("user_id");
		$data['username']=session("username");
		$data['taocan']=$taocan;
		$data['money']=$money;
		$data['taocan_num']=$_POST['authorizationtype'];
		$data['day_num']=$_POST['day_num'];
		$data['code']=get_order_str(5);
		$data['add_time']=time();
		$data['caizhong']=session("caizhong");
		$data['caizhong_num']=session("caizhong_num");
		if($data['taocan_num']==6){
			$data['pay_type']=3;
			$data['order_statue']=2;
		}
		
		$order_id =$orderModel->add($data);
		if($order_id){
			//获取列表数据
			if($data['taocan_num']==6){
				$this->batch_summary($data['user_id']);
				$this->redirect ( 'pay_list');
			}else{
				$this->redirect ( 'pay_bean', array('id'=>$order_id));
			}
		}else{
			echo "<script>alert('订单生成失败!');location.href='" . __CONTROLLER__ . "/pay'</script>";
		}
	}
	function pay_list() {
		
		$this->check_pay_login ();
		$orderModel = D("Order");
		$where_select['user_id']=session("user_id");
		$list = $orderModel->where($where_select)->order("id desc")->limit(10)->select();
		$this->assign("list",$list);
		$this->display ();
	}
	
	function pay_bean(){
		$this->check_pay_login ();
		$orderModel=D("Order");
		$where_find['id']=intval(I("id"));
		$bean = $orderModel->where($where_find)->find();
		$this->assign("bean",$bean);
		$this->display();
	}
	
	function contact_qq(){
// 		dump(1505568720-2592000);
// 		dump(date('Y-m-d H:i:s',1502976720));
		$this->display();
	}
	
	
	function has_alipay(){
		$this->check_pay_login ();
		$orderModel=D("Order");
		$where['id']=intval(I("id"));
		$order_info = $orderModel->where($where)->field("order_statue")->find();
		if(!$order_info['order_statue']){
			$data['id']=intval(I("id"));
			$data['pay_type']=1;//支付宝
			$data['order_statue']=1;
			$data['pay_time']=time();
			$bean = $orderModel->save($data);
			$this->redirect("contact_qq");
		}else{
			$this->redirect("pay_list");
		}
	}
	
	function has_weixinpay(){
		$this->check_pay_login ();
		$orderModel=D("Order");
		$where['id']=intval(I("id"));
		$order_info = $orderModel->where($where)->field("order_statue")->find();
		if(!$order_info['order_statue']){
				$data['id']=intval(I("id"));
				$data['pay_type']=2;//微信
				$data['order_statue']=1;//已支付
				$data['pay_time']=time();
				$bean = $orderModel->save($data);
				$this->redirect("contact_qq");
			}else{
				$this->redirect("pay_list");
			}
	}
	function check_pay_login() {
		if (session ( "username" ) && session ( "user_id" )) {
		} else {
			$this->redirect ( 'login_pay' );
		}
	}
	
	function logout(){
		session("user_id",null);
		session("username",null);
		$this->redirect ( 'login_pay' );
	}
	
	// 审核通过
	function batch_summary($user_id) {
			$datetime = new \DateTime ();
			$datetime->modify ( "+1 day" );
			$time_num = $datetime->format ( 'U' ) - time ();
				
			// 增加用户过期时间
			$userModel = D ( "User" );
			$where_user ['id'] = $user_id;
			$user_info = $userModel->where ( $where_user )->find ();
			
			if ($user_info ['end_time'] <= time ()) {
				$data ['end_time'] = time () + $time_num;
			} else {
				$data ['end_time'] = $user_info ['end_time'] + $time_num;
			}
			// 开启事物
			$data['id']=$user_info['id'];
			$res_save_user = $userModel->save ( $data );
	}
	
	
	//检查代理是否已注册
	public function checkr_agent_egister(){
		$username= mysql_real_escape_string(I("username"));
		$agentModel = D("Agent");
		$where['username']=$username;
		$result = $agentModel->where($where)->find();
		if($result){
			echo 2;
		}else{
			echo 1;
		}
	}
	
	//代理找回密码下一步
// 	public function check_agent_phone(){
// 		$username= mysql_real_escape_string(I("username"));
// 		$phone= mysql_real_escape_string(I("phone"));
// 		$agentModel = D("Agent");
// 		$where['username']=$username;
// 		$where['phone']=$phone;
// 		$result = $agentModel->where($where)->find();
// 		if($result){
// 			echo 1;
// 		}else{
// 			echo 2;
// 		}
// 	}
	

	
	//代理注册
	public function agent_register_post() {
		// dump($_POST);
		// die();
// 		$Verify = new \Think\Verify ();
		$Smsverif = new SmsController();
		$check_result = $Smsverif->checkCode(I('phone'),I('code'));
		if ($check_result) {
			if (I ( 'username' ) == null  || I ( 'qq' ) == null || I ( 'password' ) == null) {
				echo "<script>alert('数据填写不对,请重新填写!');location.href='" . __CONTROLLER__ . "/agent_register'</script>";
				die();
			}
			if (I ( 'password' ) != I ( 'password2' ) ) {
				echo "<script>alert('数据填写不对,请重新填写!');location.href='" . __CONTROLLER__ . "/agent_register'</script>";
				die();
			}
			$agentModel = D ( "Agent" );
			// 获取当前注册过ip和邮箱，一个超过20就不给注册
			$where_register_num ['register_ip'] = get_real_ip ();
			$where_register_num ['phone'] = I ( 'phone' );
			$where_register_num ['_logic'] = 'or';
			$resigter_num = $agentModel->where ( $where_register_num )->count ();
			if ($resigter_num > 20) {
				echo "<script>alert('注册过于频繁，请稍后再试!');location.href='" . __CONTROLLER__ . "/agent_register'</script>";
			}
			$where_find ['username'] = I ( 'username' );
			if ($agentModel->where ( $where_find )->find ()) {
				echo "<script>alert('该用户已经被注册');location.href='" . __CONTROLLER__ . "/agent_register'</script>";
			} else {
				$agent_add ['username'] = I ( 'username' );
				$agent_add ['password'] = encrypt ( I ( 'password' ) );
				$agent_add ['phone'] = I ( 'phone' );
				$agent_add ['qq'] = I ( 'qq' );
				$agent_add ['grade'] = 1;
				$agent_add ['status'] = 1;
				$agent_add ['createtime'] = time();
				$agent_add ['register_ip'] = get_real_ip ();
				$agent_id = $agentModel->add ($agent_add);
				if ($agent_id) {
					$agent['agent_id'] = $agent_id;
					$agent['username'] = I ( 'username' );
					session ( 'agent', $agent );
					$save_data['agent_id']=$agent_id;
					$agent_code = 'A'.substr_replace("000000",$agent_id,-strlen($agent_id));
					$save_data['agent_code']=$agent_code;
					$agentModel->save( $save_data );
					echo "<script>alert('" . I ( 'username' ) . "注册成功！请登陆');window.top.location.href='" . __CONTROLLER__ . "/agent_login'</script>";
				} else {
					echo "<script>alert('" . I ( 'username' ) . "注册失败！');location.href='" . __CONTROLLER__ . "/agent_register'</script>";
				}
			}
		} else {
			echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/agent_register'</script>";
		}
	}
	
	
	//忘记密码下一步
	public function agent_forget_next(){
		
		if(session('forget_username')){
			$this->assign('username',session('forget_username'));
			$this->display();
		}else{
			$username=I('username');
			$phone = I('phone');
			$code = I('code');
			if($username==''||$phone==''||$code==''){
				echo "<script>alert('非法提交!');location.href='" . __CONTROLLER__ . "/agent_getpwd'</script>";
				die();
			}
			$Smsverif = new SmsController();
			$check_result = $Smsverif->checkCode($phone,$code);
			if ($check_result) {
				$where_find['phone']=$phone;
				$where_find['username']=$username;
				$agent= D('Agent') -> where($where_find) -> find();
				if($agent){
					//验证正确session存下要修改用户的账号和手机号
					session('forget_username',$username);
					session('forget_agent_id',$agent['agent_id']);
					$this->assign('username',$username);
					$this->display();
				}else{
					echo "<script>alert('" . I ( 'username' ) . "账号绑定的手机号有误!');location.href='" . __CONTROLLER__ . "/agent_getpwd'</script>";
				}
			}else {
				// 			$this->system_hint("验证码错误！", 101);
				echo "<script>alert('" . I ( 'username' ) . "验证码错误！');location.href='" . __CONTROLLER__ . "/agent_getpwd'</script>";
			}
		}
	}
	
	//重置密码
	function agent_reset_pwd(){
		$session_username = session('forget_username');
		$username=I('username');
		if($username!=$session_username){
			die();
		}
		$password = I('password');
		$reset_password = I('reset_password');
		if($password==$reset_password){
			$data_save['agent_id']=session('forget_agent_id');
			$data_save['password']=encrypt ($password);
			D('Agent')->save($data_save);
			echo "<script>alert('重置密码成功,请登录！');location.href='" . __CONTROLLER__ . "/agent_cooperation'</script>";
		}else{
			echo "<script>alert('两次输入的密码不一至！');location.href='" . __CONTROLLER__ . "/agent_forget_next'</script>";
		}
	}
	
	function test(){
		$Smsverif = new SmsController();
		$Smsverif->checkCan('13729036692', '127.0.0.1');
		
	}
	function add() {
	}
	function insert() {
	}
	function foreverdelete() {
	}
	function edit() {
	}
	function update() {
	}
	function bean() {
	}
	function sort() {
	}
	function excelImport() {
	}
	function excelOutput() {
	}
	
	function search() {
		$gongshi = 0;
		while ( $gongshi < 20 ) {
			$json_result [$gongshi] = $gongshi;
			if (count ( $json_result ) >= 30) {
				break;
			}
			$gongshi ++;
		}
	}
}